﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AntsBot;

namespace Ants
{
    public class AntLoc : Location
    {
        public int Team { get; private set; }

        public AntLoc(GameState state, int row, int col, int team)
            : base(state, row, col)
        {
            this.Team = team;
        }

        public bool IsValidMove(Direction direction)
        {
            return State.IsPassable(State.GetDestination(this, direction));
        }

        public bool Move(Direction direction)
        {
            if (this.IsValidMove(direction))
            {
                this.State.issueOrder(this, direction);
                return true;
            }

            return false;
        }


        public Goal GetGoal()
        {
            var rng = new Random((int)DateTime.Now.TimeOfDay.TotalMilliseconds % 265407);

            StrategyTypes? strat = null;
            float rnd = rng.Next(0, (int)(Strategy.STRAT_CONSTANTS.Sum() * 100)) * .01f;

            for (int i = 1; i < Strategy.STRAT_COUNT; i++)
            {
                if (rnd < Strategy.STRAT_CONSTANTS.Take(i).Sum())
                {
                    strat = (StrategyTypes)(i - 1);
                    break;
                }
            }

            if (strat == null)
                strat = (StrategyTypes)(Strategy.STRAT_COUNT - 1);

            Goal goal = null;

            if (strat == StrategyTypes.GatherFood)
                goal = new GatherFood(this).GetGoal();
            else if (strat == StrategyTypes.Fight)
                goal = new Fight(this).GetGoal();
            else if (strat == StrategyTypes.SpreadOut)
                goal = new SpreadOut(this).GetGoal();
            else if (strat == StrategyTypes.Scatter)
                goal = new Scatter(this).GetGoal();
            else if (strat == StrategyTypes.Scout)
                goal = new Scout(this).GetGoal();
            else if (strat == StrategyTypes.Condense)
                goal = new Condense(this).GetGoal();
            else if (strat == StrategyTypes.Retreat)
                goal = new Retreat(this).GetGoal();

            if (goal == null)
            {
                goal = new GatherFood(this).GetGoal();
            }
            if (goal == null)
            {
                goal = new Scout(this).GetGoal();
            }
            if (goal == null)
            {
                goal = new Fight(this).GetGoal();
            }
            if (goal == null)
            {
                goal = new SpreadOut(this).GetGoal();
            }
            if (goal == null)
            {
                goal = new Scatter(this).GetGoal();
            }
            if (goal == null)
            {
                goal = new Scatter(this).GetGoal();
            }
            if (goal == null)
            {
                goal = new Scatter(this).GetGoal();
            }

            return goal;
        }
    }
}
